package com.xing.lei.happy.logic;

import com.xing.lei.happy.utils.DataSourceUtil;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

/**
 * 表数据比较使用示例
 */
public class TableDataComparisonExample {
    
    /**
     * 比较两个数据源中相同表的数据
     * @param sourceDataSource 源数据源
     * @param targetDataSource 目标数据源
     * @param tableName 表名
     */
    public static void compareTable(DataSource sourceDataSource, DataSource targetDataSource, String tableName) {
        try {
            TableDataComparator comparator = new TableDataComparator(sourceDataSource, targetDataSource);
            
            // 自动获取主键列并执行比较
            String script = comparator.compareTableData(tableName);
            
            System.out.println("生成的SQL脚本:");
            System.out.println(script);
            
        } catch (SQLException e) {
            System.err.println("比较表数据时出错: " + e.getMessage());
            System.err.println("请确保表存在且具有主键约束");
            e.printStackTrace();
        }
    }
    
    /**
     * 比较两个数据源中相同表的数据（手动指定主键列）
     * @param sourceDataSource 源数据源
     * @param targetDataSource 目标数据源
     * @param tableName 表名
     * @param keyColumns 主键列名列表
     */
    public static void compareTableWithKeys(DataSource sourceDataSource, DataSource targetDataSource, 
                                          String tableName, List<String> keyColumns) {
        try {
            TableDataComparator comparator = new TableDataComparator(sourceDataSource, targetDataSource);
            
            // 执行比较并生成SQL脚本
            String script = comparator.compareTableData(tableName, keyColumns);
            
            System.out.println("生成的SQL脚本:");
            System.out.println(script);
            
        } catch (SQLException e) {
            System.err.println("比较表数据时出错: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    /**
     * 创建数据源示例
     */
    public static void createDataSourcesExample() {
        try {
            // 示例1: 创建Oracle数据源
            String sourceUrl = "jdbc:oracle:thin:@172.16.7.146:1521:orcl";
            String sourceUsername = "RHM_WXP_CLOSED";
            String sourcePassword = "RHM_WXP_CLOSED";

            String targetUrl = "jdbc:dm://172.16.9.162:5236?keyWords=(PERCENT)";
            String targetUsername = "RHM_DEV";
            String targetPassword = "RHM_DEV";
            
            DataSource sourceDataSource = DataSourceUtil.createOracleDataSource(sourceUrl, sourceUsername, sourcePassword);
            DataSource targetDataSource = DataSourceUtil.createDamengDataSource(targetUrl, targetUsername, targetPassword);
            
            // 测试连接
            if (DataSourceUtil.testConnection(sourceDataSource) && DataSourceUtil.testConnection(targetDataSource)) {
                System.out.println("数据源连接成功");
                
                // 执行表比较
                compareTable(sourceDataSource, targetDataSource, "XT_SYBM");
            } else {
                System.err.println("数据源连接失败");
                System.err.println("请检查数据库连接参数是否正确，包括URL、用户名和密码");
            }
            
            // 示例2: 创建带额外属性的Oracle数据源和达梦数据源
            Properties properties = new Properties();
            properties.setProperty("oracle.jdbc.ReadTimeout", "30000");
            properties.setProperty("oracle.jdbc.ConnectionTimeout", "10000");
            
            DataSource sourceDataSourceWithProps = DataSourceUtil.createOracleDataSource(
                    sourceUrl, sourceUsername, sourcePassword, properties);
            
            Properties dmProperties = new Properties();
            dmProperties.setProperty("loginTimeout", "10");
            DataSource targetDataSourceWithProps = DataSourceUtil.createDamengDataSource(
                    targetUrl, targetUsername, targetPassword, dmProperties);
            
            // 测试连接
            if (DataSourceUtil.testConnection(sourceDataSourceWithProps) && DataSourceUtil.testConnection(targetDataSourceWithProps)) {
                System.out.println("带属性的数据源连接成功");
                
                // 执行表比较
                compareTableWithKeys(sourceDataSourceWithProps, targetDataSourceWithProps, 
                                   "XT_SYBM", Arrays.asList("ID"));
            } else {
                System.err.println("带属性的数据源连接失败");
                System.err.println("请检查数据库连接参数是否正确，包括URL、用户名和密码");
            }
            
        } catch (SQLException e) {
            System.err.println("创建数据源时出错: " + e.getMessage());
            System.err.println("请确保数据库服务正在运行且连接参数正确");
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        // 运行示例
        try {
            createDataSourcesExample();
        } catch (Exception e) {
            System.err.println("程序执行过程中发生异常: " + e.getMessage());
            e.printStackTrace();
        }
    }
}